package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao1;
import com.itheima.dao.RoleDao;
import com.itheima.dao.RoleDao1;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.*;
import com.itheima.utils.IsCh;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;

/**
 * @author Tony
 * @create 2021-12-07 16:43
 */
@Service(interfaceClass = RoleService1.class)
@Transactional
public class RoleServiceImpl1 implements RoleService1 {
    @Autowired
    private RoleDao1 roleDao;

    @Autowired
    private MenuService1 menuService1;

    @Autowired
    private MenuDao1 menuDao;

    @Autowired
    private PermissionService1 permissionService;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        boolean isChinese = IsCh.isChinese(queryPageBean.getQueryString());
        String keyword = null;
        String name = null;
        if(isChinese){
            name=queryPageBean.getQueryString();
        }else{
            keyword=queryPageBean.getQueryString();
        }
        Page<Map> page = roleDao.selectByCondition(keyword,name);
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public void addRole(Role role, Integer[] menuIds, Integer[] permissionIds) {
        if(role!=null){
            roleDao.addRole(role);
            Integer roleId = role.getId();
            for (Integer menuId : menuIds) {
                Integer parentId = menuDao.findParentIdByMenuId(menuId);
                if(parentId!=null){
                    Integer exit = menuDao.isExit(parentId,roleId);
                    if(exit==null){
                        menuDao.addMenu(roleId,parentId);
                    }
                }
            }
            if(menuIds.length>0){
                menuService1.addForRole(roleId,menuIds);
            }
            if(permissionIds.length!=0){
                permissionService.addForRole(roleId,permissionIds);
            }
        }
    }

    @Override
    public Role findRoleById(Integer roleId) {
        return roleDao.findRoleById(roleId);
    }

    @Override
    public void deleteRoleById(Integer roleId) {
        permissionService.deleteRoleAndPermissionById(roleId);
        menuService1.deleteRoleAndMenuById(roleId);
        roleDao.deleteRoleById(roleId);
    }


    @Override
    public void updateRole(Role role) {
        roleDao.updateRole(role);
    }

    @Override
    public void updateMenuAndRole(Integer[] menuIds1, Integer roleId) {
        menuService1.deleteMenuById(roleId);
        Arrays.stream(menuIds1).forEach(menuId1 -> {
            Integer parentId = menuDao.findParentIdByMenuId(menuId1);
            if (parentId != null) {
                Integer exit = menuDao.isExit(parentId, roleId);
                if (exit == null) {
                    menuDao.addMenu(roleId, parentId);
                }
            }
        });
        if(menuIds1.length>0){
            menuService1.addForRole(roleId,menuIds1);
        }
    }

    @Override
    public void updatePermission(Integer[] permissionIds1, Integer roleId) {
        permissionService.deletePermissionById(roleId);
        for (Integer permissionId1 : permissionIds1) {
            permissionService.addPermissionById(permissionId1,roleId);
        }
    }
}
